<template>
  <div class="searchedView">
    <div class="header">
      <div class="left" @click="back">
        <van-icon name="arrow-left" size="25" />
      </div>
      <div class="search">
        <van-icon name="search" size="23" color="#a2a2a2" />
        <input
          type="text"
          placeholder="请输入"
          v-model="searchText"
          @focus="searchFocus"
          @input="searching"
        />
        <div v-if="isShowEmpty" class="empty" @click="empty">
          <van-icon name="cross" size="10" color="white" />
        </div>
      </div>
      <div class="right" @click="searched">搜索</div>
    </div>
    <div class="likeheader"></div>
    <div v-if="!isShowEmpty" class="searched-navBox">
      <div class="searched-nav">
        <RouterLink :to="`/searched/searchedCookbook?kw=${searchText}`" replace
          >菜谱</RouterLink
        >
      </div>
      <div class="searched-nav">
        <RouterLink :to="`/searched/searchedNote?kw=${searchText}`" replace
          >笔记</RouterLink
        >
      </div>
      <div class="searched-nav">
        <RouterLink :to="`/searched/searchedUser?kw=${searchText}`" replace
          >用户</RouterLink
        >
      </div>
      <div class="searched-nav">
        <RouterLink :to="`/searched/searchedCommodity?kw=${searchText}`" replace
          >商品</RouterLink
        >
      </div>
      <div class="searched-nav">
        <RouterLink
          :to="`/searched/searchedCurriculum?kw=${searchText}`"
          replace
          >课堂</RouterLink
        >
      </div>
    </div>
    <RouterView v-if="!isShowEmpty" />
    <div
      class="suggests"
      v-else-if="searchSuggests.suggests.length > 0 && isShowEmpty"
    >
      <searchSuggest
        v-for="(suggest, index) in searchSuggests.suggests"
        :key="index"
        :suggest="suggest"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import searchSuggest from "@/components/searchSuggest.vue";
import router from "@/router";
import { onMounted, ref, watch, type Ref } from "vue";
import { storeToRefs } from "pinia";
import { useRouter } from "vue-router";
import { useDouguoStore } from "@/stores/douguo";
import lodash from "lodash";

const douguo = useDouguoStore();

const { searchSuggests } = storeToRefs(douguo);
const { updataSearch, updataSearchSuggests } = douguo;

const route: object = useRouter();
// console.log(route);

const searchText = ref(route.currentRoute.value.query.kw);

const obj1: object = ref({
  keyword: searchText.value,
  order: 0,
  type: 0,
  offset: 0 * 20,
  limit: 1 * 20,
});

updataSearch(obj1);

const back = () => {
  // console.log("back");
  history.go(-1);
};

const isShowEmpty: Ref<boolean> = ref(false);

const searchFocus = () => {
  // console.log("searchFocus");
  isShowEmpty.value = true;
  updataSearchSuggests(searchText.value);
};

const empty = () => {
  // console.log("empty");
  searchText.value = "";
  history.go(-1);
};

const searching = lodash.debounce(() => {
  if (searchText.value) {
    // console.log("searching", searchText.value);
    updataSearchSuggests(searchText.value);
  }
}, 500);

const searched = () => {
  // console.log("searched", searchText.value);
  // 页面跳转
  router.replace({ path: "/searched", query: { kw: searchText.value } });

  // 更新搜索结果
  let obj1: object = ref({
    keyword: searchText.value,
    order: 0,
    type: 0,
    offset: 0 * 20,
    limit: 1 * 20,
  });
  updataSearch(obj1);

  // 添加历史记录
  let historySearch = new Set(
    JSON.parse(localStorage.getItem("historySearch") || "[]")
  );
  // console.log(historySearch);
  historySearch.add(searchText.value);
  localStorage.setItem(
    "historySearch",
    JSON.stringify(Array.from(historySearch))
  );
  isShowEmpty.value = false;
};

// console.log(route.currentRoute.value.query.kw);

watch(
  () => router.currentRoute.value.query.kw,
  (newValue: any) => {
    // console.log("newValue", newValue);
    isShowEmpty.value = false;
    searchText.value = newValue;
  }
  // { immediate: true }
);
</script>

<style lang="scss" scoped>
.searchedView {
  // padding: 0 13px;

  .likeheader {
    height: 40px;
    margin-top: 10px;
    margin-bottom: 20px;
  }

  .header {
    width: 100%;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 10px;
    padding-bottom: 10px;
    position: fixed;
    left: 0;
    top: 0;
    background-color: white;
    z-index: 10;

    .left {
      // margin-left: 10px;
      cursor: pointer;
      margin-left: 13px;
    }

    .search {
      width: 244px;
      height: 40px;
      border-radius: 10px;
      background-color: #f6f6f6;
      display: flex;
      align-items: center;
      padding-left: 14px;

      input {
        background: none;
        outline: none;
        border: 0px;
        color: #a2a2a2;
        margin-left: 5px;
        height: 20px;
        line-height: 20px;
        padding: 0;
        padding-bottom: 3px;
        width: 170px;
      }

      input[type="text"]:focus,
      input[type="password"]:focus {
        background: none;
        outline: none;
      }

      .empty {
        height: 16px;
        width: 16px;
        border-radius: 50%;
        background-color: #cecece;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 20px;
        cursor: pointer;
      }
    }

    .right {
      // margin-right: 15px;
      cursor: pointer;
      margin-right: 15px;
    }
  }

  .searched-navBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 42px 8px;
    border-bottom: 1px solid #ebebeb;

    .searched-nav {
      font-size: 14px;

      a {
        color: #999;
      }

      .router-link-active {
        color: black;
        font-weight: bold;
      }
    }
  }

  .suggests {
    display: flex;
    flex-direction: column;
    padding: 0 13px;
  }
}
</style>
